package servlets;

import java.io.IOException;
import java.sql.SQLException;

import javax.servlet.*;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import eccezioni.UtenteNonTrovatoException;
import gui_interfaces.CommonInterface;

import businessLogic.CommonInterfaceMySQL;

import Beans.Agente;

public class AddCategoria extends HttpServlet {
	
	 public void goToPage(String url,HttpServletRequest request,HttpServletResponse response)
     throws  ServletException, IOException{

          RequestDispatcher dispatcher= getServletContext().getRequestDispatcher(url);
          dispatcher.forward(request,response);

     }
	 
	 public void doGet(HttpServletRequest request,HttpServletResponse response ) throws
     ServletException, IOException, NullPointerException {

                   doPost(request,response);
                                  }
	 
	 public void doPost(HttpServletRequest req, HttpServletResponse res) throws
     IOException, ServletException {

          try { 
        	  HttpSession session= req.getSession(true);
              String macro = req.getParameter("macro");
              String categoria = req.getParameter("categoria");
            CommonInterface  ci = new CommonInterfaceMySQL(macro);
              ci.addCategoria(categoria.trim(), macro.trim());
              String pagina = macro.toLowerCase()+"Ins.jsp?msg= Aggiunto: "+categoria+ "Macro: "+macro;
              res.sendRedirect(pagina.trim());
       
         }
             
                                        
             catch (UtenteNonTrovatoException t){
            	 res.sendRedirect("errore.jsp?errore=AddCategoria Nome Utente o password errati");
             }
             catch(NullPointerException n){
            	 res.sendRedirect("errore.jsp?errore=AddCategoriaEffettuare il LogIng"+n.getMessage()+n.getStackTrace());

             } catch (SQLException e) {
				// TODO Auto-generated catch block
            	 res.sendRedirect("errore.jsp?errore=AddCategoria: SQLExceptio  "+e.getStackTrace()+"\n"+e.getMessage());
			} catch (ClassNotFoundException e) {
				// TODO Auto-generated catch block
				res.sendRedirect("errore.jsp?errore=addCategoria: ClassNotFount "+e.getStackTrace()+"\n"+e.getMessage());
			}
        
             
          

         }

}
